﻿using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Migrations;

namespace IndoorPosition.Server.Database.Migrations.Migrations
{
    public partial class v002 : Migration
    {
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.AddColumn<int>(
                name: "FloorId",
                table: "Station",
                type: "int",
                nullable: false,
                defaultValue: 0,
                comment: "所属楼层ID");

            migrationBuilder.CreateTable(
                name: "Project",
                columns: table => new
                {
                    Id = table.Column<int>(type: "int", nullable: false)
                        .Annotation("MySql:ValueGenerationStrategy", MySqlValueGenerationStrategy.IdentityColumn),
                    Name = table.Column<string>(type: "varchar(50)", maxLength: 50, nullable: true, comment: "项目名称")
                        .Annotation("MySql:CharSet", "utf8mb4"),
                    MinLongitude = table.Column<double>(type: "double", nullable: false, comment: "项目范围：最西"),
                    MaxLongitude = table.Column<double>(type: "double", nullable: false, comment: "项目范围：最东"),
                    MinLatitude = table.Column<double>(type: "double", nullable: false, comment: "项目范围：最南"),
                    MaxLatitude = table.Column<double>(type: "double", nullable: false, comment: "项目范围：最北"),
                    SceneUrl = table.Column<string>(type: "varchar(200)", maxLength: 200, nullable: true, comment: "场景URL")
                        .Annotation("MySql:CharSet", "utf8mb4"),
                    TerrainUrl = table.Column<string>(type: "varchar(200)", maxLength: 200, nullable: true, comment: "地形URL")
                        .Annotation("MySql:CharSet", "utf8mb4"),
                    MapUrl = table.Column<string>(type: "varchar(200)", maxLength: 200, nullable: true, comment: "地图URL")
                        .Annotation("MySql:CharSet", "utf8mb4"),
                    Route = table.Column<string>(type: "varchar(50)", maxLength: 50, nullable: true, comment: "室外路径")
                        .Annotation("MySql:CharSet", "utf8mb4")
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_Project", x => x.Id);
                },
                comment: "项目")
                .Annotation("MySql:CharSet", "utf8mb4");

            migrationBuilder.CreateTable(
                name: "Building",
                columns: table => new
                {
                    Id = table.Column<int>(type: "int", nullable: false)
                        .Annotation("MySql:ValueGenerationStrategy", MySqlValueGenerationStrategy.IdentityColumn),
                    Name = table.Column<string>(type: "longtext", nullable: true, comment: "建筑物名称")
                        .Annotation("MySql:CharSet", "utf8mb4"),
                    X0 = table.Column<double>(type: "double", nullable: false, comment: "模型参照点X坐标"),
                    Y0 = table.Column<double>(type: "double", nullable: false, comment: "模型参照点Y坐标"),
                    X = table.Column<double>(type: "double", nullable: false, comment: "模型坐标X0对应的WGS84墨卡托投影坐标"),
                    Y = table.Column<double>(type: "double", nullable: false, comment: "模型坐标Y0对应的WGS84墨卡托投影坐标"),
                    Model = table.Column<string>(type: "varchar(50)", maxLength: 50, nullable: true, comment: "模型")
                        .Annotation("MySql:CharSet", "utf8mb4"),
                    ProjectId = table.Column<int>(type: "int", nullable: false, comment: "项目ID")
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_Building", x => x.Id);
                    table.ForeignKey(
                        name: "FK_Building_Project_ProjectId",
                        column: x => x.ProjectId,
                        principalTable: "Project",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                },
                comment: "建筑")
                .Annotation("MySql:CharSet", "utf8mb4");

            migrationBuilder.CreateTable(
                name: "Floor",
                columns: table => new
                {
                    Id = table.Column<int>(type: "int", nullable: false)
                        .Annotation("MySql:ValueGenerationStrategy", MySqlValueGenerationStrategy.IdentityColumn),
                    Name = table.Column<string>(type: "longtext", nullable: true, comment: "楼层名称")
                        .Annotation("MySql:CharSet", "utf8mb4"),
                    Index = table.Column<int>(type: "int", nullable: false, comment: "楼层序号"),
                    Model = table.Column<string>(type: "longtext", nullable: true, comment: "模型")
                        .Annotation("MySql:CharSet", "utf8mb4"),
                    Route = table.Column<string>(type: "longtext", nullable: true, comment: "路径")
                        .Annotation("MySql:CharSet", "utf8mb4"),
                    BuildingId = table.Column<int>(type: "int", nullable: false, comment: "建筑物ID")
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_Floor", x => x.Id);
                    table.ForeignKey(
                        name: "FK_Floor_Building_BuildingId",
                        column: x => x.BuildingId,
                        principalTable: "Building",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                },
                comment: "楼层")
                .Annotation("MySql:CharSet", "utf8mb4");

            migrationBuilder.CreateIndex(
                name: "IX_Station_FloorId",
                table: "Station",
                column: "FloorId");

            migrationBuilder.CreateIndex(
                name: "IX_Building_ProjectId",
                table: "Building",
                column: "ProjectId");

            migrationBuilder.CreateIndex(
                name: "IX_Floor_BuildingId",
                table: "Floor",
                column: "BuildingId");

            migrationBuilder.AddForeignKey(
                name: "FK_Station_Floor_FloorId",
                table: "Station",
                column: "FloorId",
                principalTable: "Floor",
                principalColumn: "Id",
                onDelete: ReferentialAction.Cascade);
        }

        protected override void Down(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropForeignKey(
                name: "FK_Station_Floor_FloorId",
                table: "Station");

            migrationBuilder.DropTable(
                name: "Floor");

            migrationBuilder.DropTable(
                name: "Building");

            migrationBuilder.DropTable(
                name: "Project");

            migrationBuilder.DropIndex(
                name: "IX_Station_FloorId",
                table: "Station");

            migrationBuilder.DropColumn(
                name: "FloorId",
                table: "Station");
        }
    }
}
